<?php

defined('SYSPATH') or die('No direct script access.');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Controller_Portal extends Controller_WebInterface {

    protected $_active_menu;
    protected $_logined = 0;
    protected $_user;
    protected $_node_xml;
    protected $_directory;
    protected $_controller;
    protected $_action;
    protected $_iframe;
    protected $_callback;
    protected $_bar_right;
    protected $_bar_left;

    public function before() {
        $this->setTemplate("portal");
        $this->setActive_menu('Home');
        $this->setbar_right('');
        View::bind_global('bar_right', $this->_bar_right);
        $this->setbar_left('');
        View::bind_global('bar_left', $this->_bar_left);
        parent::before();
        $this->_iframe = Arr::get($_GET, 'iframe', 0);
        $this->_callback = Arr::get($_GET, 'callback', '');
        View::bind_global('iframe', $this->_iframe);
        View::bind_global('callback', $this->_callback);
        $this->_controller = strtolower($this->request->controller());
        
        $session_user = Controller_Helper_Authentication::get_session();
        $session_user = empty($session_user)?new Domain_Entities_User():$session_user;
        
        if ($session_user instanceof Domain_Entities_User) {
            if ($session_user->getId() > 0) {
                $this->_logined = 1;
            }else{
                $ip_config = Kohana::$config->load('ip_config');
                $a_location_ip = $ip_config->ip;
                $location_ip = $_SERVER['REMOTE_ADDR'];
                if(array_key_exists($location_ip, $a_location_ip)){
                    $session_user->setLocation_Id($a_location_ip[$location_ip]);
                    $this->_logined = 2;
                }
            }
        }
        
        if($this->_logined == 0 && $this->_controller != "auth"){
            $this->redirect(URL::base().'login.html');
        }
        
        $this->_user = $session_user;
        View::bind_global('_user', $session_user);
        View::bind_global('_logined', $this->_logined);
    }

    public function check_security($token) {
        if ($token != Security::token()) {
            $this->redirect(URL::base() . 'user/login/logout');
        }
    }

}

?>